﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PM.Controls;
using HealthManagement.Data.Model;
using HealthManagement.Data.Service;
using Telerik.Windows.Controls;
using PM.Controls.Common;

namespace HealthManagement.View
{
    /// <summary>
    /// UserForm.xaml 的交互逻辑
    /// </summary>
    public partial class UserForm : PMWindow
    {
        MeasureUser user = new MeasureUser();
        public event Action onSaveHandler;
        public UserForm()
        {
            InitializeComponent();
            Sex.ItemsSource = new string[]{"男","女"};
            BloodType.ItemsSource = new string[] { "A", "B", "AB", "O" };
            this.DataContext = user;
        }

        public UserForm(string userid) 
        {
            InitializeComponent();
            Sex.ItemsSource = new string[] { "男", "女" };
            BloodType.ItemsSource = new string[] { "A", "B", "AB", "O" };
            UserService ser = new UserService();
            user = ser.LoadMeasureUserList(c=>c.userId == userid).Single();
            this.DataContext = user;
        }

        public UserForm(string userid,Action a) : this(userid)
        {
            onSaveHandler += a;
        }

        public UserForm(string userid,bool isEnabled) : this(userid)
        {
            btnSave.Visibility = btnCancel.Visibility = Visibility.Hidden;

            var children = mainBorder.ChildrenOfType<StackPanel>();
            foreach (var child in children)
            {
                var c1 = child.FindChildByType<RadMaskedTextBox>();
                if (c1 != null)
                    c1.IsEnabled = isEnabled;

                var c2 = child.FindChildByType<RadComboBox>();
                if (c2 != null)
                    c2.IsEnabled = isEnabled;

                var c3 = child.FindChildByType<RadDatePicker>();
                if (c3 != null)
                    c3.IsEnabled = isEnabled;

            }  
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {

            UserService ser = new UserService();

            if (string.IsNullOrEmpty(user.userId))
            {
                user.userId = Guid.NewGuid().ToString().Substring(0, 20);
                user.createDate = DateTime.Now;
                user.ModifyDate = DateTime.Now;

                int idcard = ser.GetMeasureUserMaxIdCard();
                user.IdCard = (idcard+1).ToString("0000");

                ser.Add(user);
                Tools.ShowMessage(true,string.Format("新增成功，服务编号为：{0}",user.IdCard));
            }
            else
            {
                user.ModifyDate = DateTime.Now;
                ser.Update(user, "userId", "userId");
            }

            this.Close();
            if (onSaveHandler != null)
                onSaveHandler();
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            this.CloseWindow();
        }

        private void Sex_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string val = Sex.SelectionBoxItem.ToString() ;
            if (user.PhotoPath == null)
            {
                if (val == "男")
                {
                    photo1.Source = photo2.Source = new BitmapImage(new Uri("/Images/Male128.png", UriKind.Relative));
                }
                if (val == "女")
                {
                    photo1.Source = photo2.Source = new BitmapImage(new Uri("/Images/female128.png", UriKind.Relative));
                }
            }
        }
    }
}
